<template>
  <div v-loading="loading">
    <a-form-model
      ref="wechatForm"
      :rules="wechatRules"
      :model="wechatInfo"
      :labelCol="{ span: 3 }"
      :wrapperCol="{ span: 12 }"
    >
      <a-form-model-item label="商家小程序APPID" prop="platform_mch_appid">
        <a-input v-model="wechatInfo.platform_mch_appid" type="text" />
      </a-form-model-item>
      <a-form-model-item label="商家小程序Secret" prop="platform_mch_key">
        <a-input v-model="wechatInfo.platform_mch_key" type="text" />
      </a-form-model-item>
      <a-form-model-item label='商户号' prop='platform_mch_id'>
        <a-input v-model='wechatInfo.platform_mch_id' type='text' />
      </a-form-model-item>
      <a-form-model-item label='kpay_ey' prop='platform_pay_key'>
        <a-input v-model='wechatInfo.platform_pay_key' type='text' />
      </a-form-model-item>
      <a-form-model-item label='apiclient_cert' prop='platform_apiclient_cert'>
        <a-input v-model='wechatInfo.platform_apiclient_cert' :autoSize='{ minRows: 4, maxRows: 6 }'
                 type='textarea' />
      </a-form-model-item>
      <a-form-model-item label='apiclient_key' prop='platform_apiclient_key'>
        <a-input v-model='wechatInfo.platform_apiclient_key' :autoSize='{ minRows: 4, maxRows: 6 }'
                 type='textarea' />
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 24 }">
        <a-divider style="margin: 0 0 40px 0" />
        <div style="display: flex; justify-content: center">
          <a-button :loading="btnLoading" type="primary" @click="submitForm" style="margin-right: 8px"> 保存 </a-button>
          <a-button @click="$router.back()"> 取消 </a-button>
        </div>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
import { getPlatformWechatMchSetting, setPlatformWechatMchSetting } from '@/api/saas/setting'

export default {
  name: 'Mch',
  data () {
    return {
      loading: false,
      btnLoading: false,
      wechatInfo: {
        platform_mch_appid: '',
        platform_mch_key: '',
        platform_mch_id: '',
        platform_apiclient_cert: '',
        platform_apiclient_key: '',
        platform_pay_key: '',
      },
      wechatRules: {
        platform_mch_appid: [{ required: true, message: '请输入联盟小程序APPID', trigger: 'blur' }],
        platform_mch_key: [{ required: true, message: '请输入联盟小程序Secret', trigger: 'blur' }],
        platform_mch_id: [{ required: true, message: '请输入服务商商户号', trigger: 'blur' }],
        platform_apiclient_cert: [{ required: true, message: '请输入服务商apiclient_cert证书', trigger: 'blur' }],
        platform_apiclient_key: [{ required: true, message: '请输入服务商apiclient_key证书', trigger: 'blur' }],
      },
      // 微信模板列表
      templateList: [],
    }
  },
  created () {
    this.getData()
    // this.getTemplateList()
  },
  methods: {
    getData: function () {
      this.loading = true
      getPlatformWechatMchSetting({})
        .then((res) => {
          if (res.code == 0) {
            this.wechatInfo = res.data
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.loading = false
        })
    },
    submitForm: function () {
      this.$refs.wechatForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          setPlatformWechatMchSetting(this.wechatInfo)
            .then((res) => {
              if (res.code == 0) {
                this.$message.success('保存成功！')
              } else {
                this.$message.warning(res.msg ? res.msg : '保存失败！')
              }
            })
            .catch((error) => {
              console.log(error)
            })
            .then(() => {
              this.btnLoading = false
            })
        }
      })
    },
    // 获取微信模板列表
    // async getTemplateList() {
    //   const res = await templateList()
    //   if (res.code == 0) {
    //     this.templateList = res.data.template_list
    //   }
    // },
  },
}
</script>

<style scoped>
</style>
